package edu.zjut.hjg.saas.kwmp.web.controller.home;

import static edu.zjut.hjg.saas.kwmp.web.support.KwmpConstants.PAGE_SIZE;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import edu.zjut.hjg.saas.biz.model.Album;
import edu.zjut.hjg.saas.biz.model.Kindergarten;
import edu.zjut.hjg.saas.biz.service.ActivityService;
import edu.zjut.hjg.saas.biz.service.AlbumService;
import edu.zjut.hjg.saas.biz.support.AlbumType;
import edu.zjut.hjg.saas.biz.support.Paginated;
import edu.zjut.hjg.saas.common.annotation.RequestAttribute;

@Controller
@RequestMapping("/home/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private AlbumService    albumService;

    @RequestMapping("/picture/list.htm")
    public void listPictures(
                             @RequestAttribute Kindergarten app,
                             @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                             @RequestParam("albumId") int albumId, Model model) {
        if (page < 1) {
            page = 1;
        }
        Integer appId = app.getId();
        model.addAttribute("album", albumService.findByIdAndAppId(albumId, appId));
        model.addAttribute("paginated", activityService.listByAlbumIdAndAppId(albumId, appId,
                PAGE_SIZE, page));
    }

    @RequestMapping("/picture/detail.htm")
    public void showPicture(@RequestAttribute Kindergarten app, @RequestParam("id") int id,
                            Model model) {
        model.addAttribute("activity", activityService.findByIdAndAppId(id, app.getId()));
    }

    @RequestMapping("/album/list.htm")
    public void listAlbums(
                           @RequestAttribute Kindergarten app,
                           @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                           Model model) {
        if (page < 1) {
            page = 1;
        }
        Paginated<Album> paginated = albumService.listByAlbumTypeAndAppId(AlbumType.ACTIVITY, app
                .getId(), PAGE_SIZE, page);
        model.addAttribute("paginated", paginated);
        model.addAttribute("covers", activityService.getAlbumCoversByAlbums(paginated.getValues()));
    }
}
